package GameEngine;

import java.io.*;
import java.nio.file.*;

import static java.nio.file.StandardCopyOption.*;

import java.util.*;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class TestReport {
	private String fileName;
	private String blackPlayer;
	private String whitePlayer;
	private int iaLevel1;
	private int iaLevel2;
	private int numActualTest;
	private String sheetName;
	private File file;
	private WritableSheet wsheet;
	private int totalNumTest;

	public TestReport(int algType1,int algType2,int iaLevel1, int iaLevel2, int numActualTest, int totalNumTest){
		this.iaLevel1=iaLevel1;
		this.iaLevel2=iaLevel2;
		this.numActualTest=numActualTest;
		this.totalNumTest = totalNumTest;
		setIAPlayer(algType1,algType2);
		createWorkSet();
	}
	
	private void createWorkSet(){
		this.fileName = blackPlayer + iaLevel1+ "vs" + whitePlayer + iaLevel2 +".xls";
		this.file = new File(fileName);
		sheetName = new String(iaLevel1 + "vs" + iaLevel2);
		
		if (numActualTest == 1) {
			WritableWorkbook wworkbook = null;
			try {
				wworkbook = Workbook.createWorkbook(file);

				wsheet = wworkbook.createSheet(sheetName, 0);
				Label label1 = new Label(0, 0, blackPlayer); // A0
				Number number1 = new Number(0, 1, iaLevel1); // A1

				Label label2 = new Label(1, 0, whitePlayer); // C0
				Number number2 = new Number(1, 1, iaLevel2); // C1

				wsheet.addCell(label1);
				wsheet.addCell(label2);

				wsheet.addCell(number1);
				wsheet.addCell(number2);

				wworkbook.write();
				wworkbook.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RowsExceededException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private void setIAPlayer(int algType1,int algType2){
		if(algType1 < 0 || algType1 > 4 || algType2 < 0 || algType2 > 4){ 
			System.out.println("Algorithm error"); 
		}else{
			switch(algType1){
			case 0:
				blackPlayer ="MinMax";
				break;
			case 1:
				blackPlayer ="AlphaBeta";
				break;
			case 2:
				blackPlayer ="Heuristic";
				break;
			case 3:
				blackPlayer ="Rondom MinMax";
				break;
			case 4:
				blackPlayer ="Random AlphaBeta";
				break;
			default:
				blackPlayer ="error";
			}

			switch(algType2){
			case 0:
				whitePlayer ="MinMax";
				break;
			case 1:
				whitePlayer ="AlphaBeta";
				break;
			case 2:
				whitePlayer ="Heuristic";
				break;
			case 3:
				whitePlayer ="Rondom MinMax";
				break;
			case 4:
				whitePlayer ="Random AlphaBeta";
				break;
			default:
				whitePlayer ="error";
			}
		}
	}

	public void writeMove(ArrayList<Double> timesBlack, ArrayList<Double> timesWhite, Status status) {
		int numMovesBlack = timesBlack.size(); 
		int numMovesWhite = timesWhite.size();
		double meanBlack = 0;
		double meanWhite = 0;
		int winner = -1;
		double gameTime = 0;
		int numMoveToWin = 0;
		double minValueBlack = 0 ;
		double maxValueBlack = 0;
		double minValueWhite = 0 ;
		double maxValueWhite = 0;
		double varBlack = 0;
		double varWhite = 0;
		File oldfile = file;
		
		for(Double d : timesBlack){
			gameTime += d.doubleValue();
			if(d.doubleValue()<minValueBlack){
				minValueBlack=d.doubleValue();
			}
			if(d.doubleValue()>maxValueBlack){
				maxValueBlack=d.doubleValue();
			}
		}
		
		varBlack = maxValueBlack - minValueBlack;
		
		for(Double d : timesWhite){
			gameTime += d.doubleValue();
			if(d.doubleValue()<minValueWhite){
				minValueWhite=d.doubleValue();
			}
			if(d.doubleValue()>maxValueWhite){
				maxValueWhite=d.doubleValue();
			}
		}
		
		varWhite = maxValueWhite - minValueWhite;
		
		if(status.value() == Status.BLACK_WINS){
			winner = -1;
			numMoveToWin = timesBlack.size()+1; //+1 because we don't take the time for the first move
		}else if(status.value() == Status.WHITE_WINS){
			winner = 1;
			numMoveToWin = timesWhite.size()+1; //+1 because we don't take the time for the first move
		}else{
			numMoveToWin = timesWhite.size()+1; //+1 because we don't take the time for the first move
			winner = 0; //pareggio
		}
		
		
		for(Double d : timesBlack){
			meanBlack += d.doubleValue();
		}
		meanBlack /= numMovesBlack;
		
		for(Double d : timesWhite){
			meanWhite += d.doubleValue();
		}
		meanWhite /=numMovesWhite;
		
		try {
			Workbook oldWorkbook = Workbook.getWorkbook(oldfile);
		
			oldfile.delete();
			
			File newFile = new File(fileName);
		    file = newFile;
			WritableWorkbook wworkbook = Workbook.createWorkbook(file, oldWorkbook);
			wsheet = wworkbook.getSheet(sheetName);
			
			Label label1 = new Label(2, 0, "Black Var");
			Label label2 = new Label(3, 0, "White Var");
			Label label3 = new Label(4, 0, "Game Time");
			Label label4 = new Label(5, 0, "Num Mosse");
			Label label5 = new Label(6, 0, "Winner");
			
			Number numBlack = new Number(0,3+numActualTest,meanBlack);
			Number numWhite = new Number(1,3+numActualTest,meanWhite);
			Number varianceBlack = new Number(2,3+numActualTest,varBlack);
			Number varianceWhite = new Number(3,3+numActualTest,varWhite);
			Number totalTime = new Number(4,3+numActualTest,gameTime);
			Number numMove = new Number(5,3+numActualTest,numMoveToWin);
			Number gameWinner = new Number(6,3+numActualTest,winner);
			
			wsheet.addCell(label1);
			wsheet.addCell(label2);
			wsheet.addCell(label3);
			wsheet.addCell(label4);
			wsheet.addCell(label5);
			wsheet.addCell(numBlack);
			wsheet.addCell(numWhite);
			wsheet.addCell(varianceBlack);
			wsheet.addCell(varianceWhite);
			wsheet.addCell(totalTime);
			wsheet.addCell(numMove);
			wsheet.addCell(gameWinner);

			
			
			
			wworkbook.write();
			wworkbook.close();
			if(this.totalNumTest == this.numActualTest){
				File newDir = new File("Test");
				Path source = Paths.get(file.toURI());
				Path target = Paths.get(newDir.getAbsolutePath()+"/"+fileName);
				Files.move(source, target, ATOMIC_MOVE);
			}
		
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
